package com.rambo.spm.sys.controller;

import com.rambo.spm.common.aop.OperationLog;
import com.rambo.spm.common.controller.BaseController;
import com.rambo.spm.sys.dao.inter.po.User;
import com.rambo.spm.sys.service.inter.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class UserController extends BaseController {

    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        Assert.notNull(userService, "userService is required!");
        this.userService = userService;
    }

    @RequestMapping("/")
    @OperationLog(operationModular = "系统用户", operationContent = "获取用户列表")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("index");
        modelAndView.addObject("userList", userService.selectList(null));
        return modelAndView;
    }

    @RequestMapping("/user/add")
    @OperationLog(operationModular = "系统用户", operationContent = "新增系统用户")
    public String addUser(User user, Model model) {
        user.setYxbz("Y");
        user.setPasswd("ICy5YqxZB1uWSwcVLSNLcB ==");
        if (userService.insert(user)) {
            model.addAttribute("userList", userService.selectList(null));
        }
        logger.info("addUser{}", user.getUuid());
        return "index";
    }

    @RequestMapping("/user/get")
    @OperationLog(operationModular = "系统用户", operationContent = "获取单个用户详情")
    public String getUser(@RequestParam(value = "userId", required = false) String userId, Model model) {
        if (userId != null) {
            model.addAttribute("user", userService.selectById(userId));
        }
        return "sys/saveUser";
    }

    @RequestMapping("/user/delete/{userId}")
    @OperationLog(operationModular = "系统用户", operationContent = "删除用户")
    public String deleteUser(Model model, @PathVariable("userId") String userId) {
        if (userService.deleteById(userId)) {
            model.addAttribute("userList", userService.selectList(null));
        }
        return "index";
    }
}
